

#include "SampleTasks.h"

#include <vector>
#include <iterator>
#include <algorithm>

using namespace ::nebula::sdk;
using namespace std;

namespace nebula { namespace sample {

NEBULA_ERROR_CODE PlainTextTask::Serialize (std::ostream& os)
{
	os.write(_content.c_str(), _content.size());
	return NEBULA_ERR_SUCCESS;
}

NEBULA_ERROR_CODE PlainTextTask::UnSerialize(std::istream& is)
{
	std::string content((istreambuf_iterator<char>(is)), istreambuf_iterator<char>());
	_content.swap(content);
	return NEBULA_ERR_SUCCESS;
}

std::string& PlainTextTask::GetContent()
{
	return _content;
}

//////////////////////////////////////////////////////////////////////////

RuntimeStatTask::RuntimeStatTask(RuntimeStat* info) :
	file_encrypted(info->file_encrypted),
	file_decrypted(info->file_decrypted),
	size_encrypted(info->size_encrypted),
	size_decrypted(info->size_decrypted)
{
}

NEBULA_ERROR_CODE RuntimeStatTask::Serialize(std::ostream& os)
{
	os <<
		"<runtime-stat>"
			"<file-encrypted>" << file_encrypted << "</file-encrypted>"
			"<file-decrypted>" << file_decrypted << "</file-decrypted>"
			"<size-encrypted>" << size_encrypted << "</size-encrypted>"
			"<size-decrypted>" << size_decrypted << "</size-decrypted>"
		"</runtime-stat>";
	return NEBULA_ERR_SUCCESS;
}

NEBULA_ERROR_CODE RuntimeStatTask::UnSerialize(std::istream& is)
{
	return NEBULA_ERR_NO_SUCH_SERVICE;
}

//////////////////////////////////////////////////////////////////////////
RuntimeStat::RuntimeStat() :
	file_encrypted(0),
	file_decrypted(0),
	size_encrypted(0),
	size_decrypted(0)
{
}

void RuntimeStat::Accumulat(EncryptFile* info)
{
	IKey* user_meta = info->GetTaskMeta()->GetUserData();
	if (user_meta->GetBoolValue("info/encrypted"))
		return AccumlateEncryption(info);
	else
		return AccumlateDecryption(info);
}

void RuntimeStat::AccumlateEncryption(EncryptFile* info)
{
	file_encrypted += 1;
	size_encrypted += info->GetContent().size();
}

void RuntimeStat::AccumlateDecryption(EncryptFile* info)
{
	file_decrypted += 1;
	size_decrypted += info->GetContent().size();
}



}} //end of nebula::sample

